#include <iostream>
#include <algorithm> //pentru functia replace
#include <cstring> //pt functia strftime
#include <fstream>
#include <stdlib.h>
#include <conio.h>
#include <ctime>
#include <iomanip>

int n;
unsigned int i,j;
using namespace std;


class membru{
public:
    int id;
    char nume[25];
    char prenume[25];
    char varsta[20];
    char email[35];
    char telefon[10];
};

class event{
    public:
    char _titlu[256];
    char _locatie[256];
    char _data[20];
    void arata_cdata(void){ // adauga data curenta in cdata
    time_t t = time(0);
    const int MAXLEN = 20;
    char _cdata[MAXLEN];
    strftime(_cdata, 20, "%d/%m/%Y", localtime(&t));
    strcpy(cdata,_cdata);
    };
    void adauga_eveniment(char _titlu[256], char _locatie[256], char _data[20]); //scrie un eveniment in fisier
    void incarca_evenimente(void); //incarca toate evenimentele din fisier
    void arata_evenimente(void); // afiseaza lista de evenimente
    int arata_eveniment(int id); // afiseaza un eveniment, functie utilizata la stergerea unui eveniment
    int sterge_eveniment(int id); // sterge un eveniment din fisier
    void eveniment_azi(void); // verifica daca exista evenimente pe data curenta
    int nr_event(void); // functie care numara evenimentele din fisier
    private:
    char cdata[20];
    char titlu[256];
    char locatie[256];
    char data[20];
    int nr;
};
event azi;

char meniu(){
    system("cls");
    cout<<"=================== APLICATIE GESTIUNE ASOCIATIE STUDENTEASCA ==================\n";
    azi.eveniment_azi();
    cout<<"< [MENIU] >\n";
    cout<<"\n---- MEMBRI:\n";
    cout<<"[L] - Afisare membri\n";
    cout<<"[C] - Cautare membru\n";
    cout<<"[A] - Adaugare membri\n";
    cout<<"[S] - Sterge membru\n";
    cout<<"\n---- EVENIMENTE:\n";
    cout<<"[E] - Lista evenimente\n";
    cout<<"[B] - Adaugare eveniment\n";
    cout<<"[D] - Sterge eveniment\n";
    cout<<"\n---- ALTELE:\n";
    cout<<"[I] - Informatii program\n";
    cout<<"[O] - Informatii autor\n";
    cout<<"[X] - Iesire program\n";
    cout<<"________________________________________________________________________________\n";
    cout<<"\n Selecteaza optiune: ";
    return getch();


}


event e[100]; // vector de obiecte
int event::nr_event(void){
    int numLines = 0;
    ifstream in("evenimente.txt");
    std::string unused;
    while ( std::getline(in, unused) ) //verifica daca o linie din fisier contine date
        ++numLines;
    in.close();
    this->nr=numLines;
    return this->nr;
}
// Adaugarea unui eveniment in fisier
void event::adauga_eveniment(char _t[256], char _l[256], char _d[20]){
    replace(&_t[0], &_t[256],' ', '_'); // functia replace inlocuieste toate spatiile din variabila _t cu _
    replace(&_l[0], &_l[256],' ', '_');
    replace(&_d[0], &_d[256],' ', '_');
    strcpy(data,_d);
    strcpy(titlu,_t);
    strcpy(locatie,_l);
    ofstream h("evenimente.txt",ios::app);
    h<<e[0].data<<" "<<e[0].titlu<<" "<<e[0].locatie<<" \n";
    h.close();
}
// citirea evenimentelor din fisier si incarcarea lor in obiectul e
void event::incarca_evenimente(void){
    nr_event();
    ifstream g("evenimente.txt",ios::in);
   for(int i=0;i<=this->nr;i++)
   {
       g>>e[i].data>>e[i].titlu>>e[i].locatie;
       replace(&e[i].titlu[0], &e[i].titlu[256],'_', ' ');
       replace(&e[i].data[0], &e[i].data[20],'_', ' ');
       replace(&e[i].locatie[0], &e[i].locatie[256],'_', ' ');
   }
   g.close();
}

// afisarea evenimentelor si tablearea lor
void event::arata_evenimente(void){
    incarca_evenimente();
    cout<<" NR"<<"| TITLU"<<setw(44)<<"| LOCATIE"<<setw(13)<<"| DATA\n";
    for(i=0;i<=36;i++)
                    cout<<"--";
    for(int i=0;i<=this->nr-1;i++)
    {
        cout<<"\n"<<setw(2)<<i<<" |"<<setw(40)<<e[i].titlu<<" |"<<setw(13)<<e[i].locatie<<" | "<<e[i].data<<" |";
    }
}
// afisarea evenimentului care are numarul egal cu numarul trimis in functie
// functie folosita la afisarea unui obiect inainte de a fi sters
int event::arata_eveniment(int id){
    incarca_evenimente(); // inainte de cautarea si afisarea evenimentului se citeste din fisier si se incarca in vectorul de obiecte
    int ok = 0;
    for(int i=0;i<=this->nr-1;i++)
    {
        if(i == id){
        cout<<"Nr: "<<i<<"\nTitlu: "<<e[i].titlu<<"\nLocatie: "<<e[i].locatie<<"\nData: "<<e[i].data<<"\n\n";
        ok = 1;
        }
    }
}
// sterge un eveniment care corespunde cu nr trimis in functie
int event::sterge_eveniment(int id){
    incarca_evenimente();
    for(i=id;i<=this->nr-1;i++)
    {
        strcpy(e[i].data,e[i+1].data);
        strcpy(e[i].titlu,e[i+1].titlu);
        strcpy(e[i].locatie,e[i+1].locatie);
    }
    ofstream h("evenimente.txt");
    for(i=0;i<=this->nr-2;i++)
    {
        replace(&e[i].titlu[0], &e[i].titlu[256],' ', '_');
        replace(&e[i].data[0], &e[i].data[20],' ', '_');
        replace(&e[i].locatie[0], &e[i].locatie[256],' ', '_');
        h<<e[i].data<<" "<<e[i].titlu<<" "<<e[i].locatie<<" \n";
    }
    h.close();
    cout<<"\n\nEvenimentul a fost sters!\n";

}
// verifica daca sunt evenimente care corespund cu data curenta si le afiseaza
void event::eveniment_azi(void){
    arata_cdata();
    incarca_evenimente();
    cout<<"Evenimente astazi [ "<<this->cdata<<" ]: \n\n";
    int ok = 0;
    for(i=0;i<=this->nr-1;i++)
    {
        if(strncmp(e[i].data,this->cdata,20) == 0){
            ok = 1;
            cout<<"- "<<e[i].titlu<<", locatie: "<<e[i].locatie<<"\n";
        }
    }
     if(ok == 0)
            cout<<"- Nu sunt!\n\n";
     else
        cout<<"\n\n";


}

membru a[100];

// se adauga membri in fisier
int salveaza_membri(int m)
{
    ofstream h("membri.txt",ios::app);
    for(i=m;i<=m+n-1;i++)
    {
        h<<a[i].id<<" "<<a[i].nume<<" "<<a[i].prenume<<" "<<a[i].varsta<<" "<<a[i].email<<" "<<a[i].telefon<<" \n";
    }
    h.close();
}
// returneaza numarul de membri (linii) din fisier
int nr(){
    int numLines = 0;
    ifstream in("membri.txt");
    std::string unused;
    while ( std::getline(in, unused) )
        ++numLines;
    in.close();
    return numLines;
}
// citeste membrii din fisier si ii incarca in vector
int incarca_membri(int m){
    ifstream g("membri.txt",ios::in);
    for(i=0;i<=m;i++)
    {
       g>>a[i].id>>a[i].nume>>a[i].prenume>>a[i].varsta>>a[i].email>>a[i].telefon;
    }
    g.close();
}
// sterge un membru din fisier
int sterge_membru(int x,int m){

     for(i=m;i<=nr()-1;i++)
     {
             strcpy(a[i].nume,a[i+1].nume);
             strcpy(a[i].prenume,a[i+1].prenume);
             strcpy(a[i].varsta,a[i+1].varsta);
             strcpy(a[i].email,a[i+1].email);
             strcpy(a[i].telefon,a[i+1].telefon);
    }
    ofstream h("membri.txt");
    for(i=0;i<=x-2;i++)
    {
        h<<a[i].id<<" "<<a[i].nume<<" "<<a[i].prenume<<" "<<a[i].varsta<<" "<<a[i].email<<" "<<a[i].telefon<<" \n";
    }
    h.close();
    cout<<"\n\nMembrul a fost sters!\n";
    }

// cautare mambru dupa nume, prenume, varsta, email id, si telefon
char cautare_membru(char x,int n){ // x este optiunea de cautare iar n numarul de membri
    char str[100];
    int ok = 0;
    system("cls");
    cout<<"=================== APLICATIE GESTIUNE ASOCIATIE STUDENTEASCA ==================\n";
    cout<<"\n--------- CAUTARE MEMBRU:\n\n";
    if(x == 'n'){
        cout<<"Ati selectat cautare dupa NUME.\n\n";
        cout<<"Introduceti numele pe care il cautati:\n= ";
        gets(str);
        system("cls");
        cout<<"=================== APLICATIE GESTIUNE ASOCIATIE STUDENTEASCA ==================\n";
        cout<<"[Cautare]--> "<<str<<": \n\n";
        cout<<"ID| "<<setw(3)<<"NUME"<<setw(15)<<"| PRENUME"<<setw(11)<<"| VARSTA"<<setw(1)<<"| EMAIL"<<setw(33)<<"| TELEFON\n";
        for(i=0;i<=37;i++)
                    cout<<"--";
        for(i=0; i<=n-1;i++){
            if(strncmp(a[i].nume,str,100) == 0){
                ok = 1;
                  cout<<"\n"<<setw(2)<<a[i].id<<"| "<<setw(10)<<a[i].nume<<"| "<<setw(10)<<a[i].prenume<<"| "<<setw(6)<<a[i].varsta<<"| "<<setw(28)<<a[i].email<<"| "<<setw(5)<<a[i].telefon;
                    cout<<"\n";
            }
        }
        if(ok == 0){
            cout<<"\n\nNumele \""<<str<<"\" nu a fost gasit in baza de date!";
        }
    }
    if(x == 'p'){
        cout<<"Ati selectat cautare dupa PRENUME.\n\n";
        cout<<"Introduceti prenumele pe care il cautati:\n= ";
        gets(str);
        system("cls");
        cout<<"=================== APLICATIE GESTIUNE ASOCIATIE STUDENTEASCA ==================\n";
        cout<<"[Cautare]--> "<<str<<": \n\n";
        cout<<"ID| "<<setw(3)<<"NUME"<<setw(15)<<"| PRENUME"<<setw(11)<<"| VARSTA"<<setw(1)<<"| EMAIL"<<setw(33)<<"| TELEFON\n";
        for(i=0;i<=37;i++)
                    cout<<"--";
        for(i=0; i<=n-1;i++){
            if(strncmp(a[i].prenume,str,100) == 0){
                ok = 1;
              cout<<"\n"<<setw(2)<<a[i].id<<"| "<<setw(10)<<a[i].nume<<"| "<<setw(10)<<a[i].prenume<<"| "<<setw(6)<<a[i].varsta<<"| "<<setw(28)<<a[i].email<<"| "<<setw(5)<<a[i].telefon;
                    cout<<"\n";
            }
        }
        if(ok == 0){
            cout<<"\n\nPrenumele \""<<str<<"\" nu a fost gasit in baza de date!";
        }
    }
    if(x == 'v'){
        cout<<"Ati selectat cautare dupa VARSTA.\n\n";
        cout<<"Introduceti varsta pe care o cautati:\n= ";
        gets(str);
        system("cls");
        cout<<"=================== APLICATIE GESTIUNE ASOCIATIE STUDENTEASCA ==================\n";
        cout<<"[Cautare]--> "<<str<<" ani: \n\n";
        cout<<"ID| "<<setw(3)<<"NUME"<<setw(15)<<"| PRENUME"<<setw(11)<<"| VARSTA"<<setw(1)<<"| EMAIL"<<setw(33)<<"| TELEFON\n";
        for(i=0;i<=37;i++)
                    cout<<"--";
        for(i=0; i<=n-1;i++){
            if(strncmp(a[i].varsta,str,100) == 0){
                ok = 1;
                cout<<"\n"<<setw(2)<<a[i].id<<"| "<<setw(10)<<a[i].nume<<"| "<<setw(10)<<a[i].prenume<<"| "<<setw(6)<<a[i].varsta<<"| "<<setw(28)<<a[i].email<<"| "<<setw(5)<<a[i].telefon;
                    cout<<"\n";
            }
        }
        if(ok == 0){
            cout<<"\n\nVarsta \""<<str<<"\" nu a fost gasita in baza de date!";
        }
    }
    if(x == 't'){
        cout<<"Ati selectat cautare dupa TELEFON.\n\n";
        cout<<"Introduceti nr de telefon pe care il cautati:\n= ";
        gets(str);
        system("cls");
        cout<<"=================== APLICATIE GESTIUNE ASOCIATIE STUDENTEASCA ==================\n";
        cout<<"[Cautare]--> "<<str<<": \n\n";
        cout<<"ID| "<<setw(3)<<"NUME"<<setw(15)<<"| PRENUME"<<setw(11)<<"| VARSTA"<<setw(1)<<"| EMAIL"<<setw(33)<<"| TELEFON\n";
        for(i=0;i<=37;i++)
                    cout<<"--";
        for(i=0; i<=n-1;i++){
            if(strncmp(a[i].telefon,str,100) == 0){
                ok = 1;
                cout<<"\n"<<setw(2)<<a[i].id<<"| "<<setw(10)<<a[i].nume<<"| "<<setw(10)<<a[i].prenume<<"| "<<setw(6)<<a[i].varsta<<"| "<<setw(28)<<a[i].email<<"| "<<setw(5)<<a[i].telefon;
                    cout<<"\n";
            }
        }
        if(ok == 0){
            cout<<"\n\nNr de telefon \""<<str<<"\" nu a fost gasit in baza de date!";
        }
    }
    if(x == 'e'){
        cout<<"Ati selectat cautare dupa EMAIL.\n\n";
        cout<<"Introduceti email-ul pe care il cautati:\n= ";
        gets(str);
        system("cls");
        cout<<"=================== APLICATIE GESTIUNE ASOCIATIE STUDENTEASCA ==================\n";
        cout<<"[Cautare]--> "<<str<<": \n\n";
        cout<<"ID| "<<setw(3)<<"NUME"<<setw(15)<<"| PRENUME"<<setw(11)<<"| VARSTA"<<setw(1)<<"| EMAIL"<<setw(33)<<"| TELEFON\n";
        for(i=0;i<=37;i++)
                    cout<<"--";
        for(i=0; i<=n-1;i++){
            if(strncmp(a[i].email,str,100) == 0){
                ok = 1;
               cout<<"\n"<<setw(2)<<a[i].id<<"| "<<setw(10)<<a[i].nume<<"| "<<setw(10)<<a[i].prenume<<"| "<<setw(6)<<a[i].varsta<<"| "<<setw(28)<<a[i].email<<"| "<<setw(5)<<a[i].telefon;
                    cout<<"\n";
            }
        }
        if(ok == 0){
            cout<<"\n\nEmailul \""<<str<<"\" nu a fost gasit in baza de date!";
        }
    }
}

